package com.yjd.pub.settlement.service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.settlement.model.ClearDayPfModel;
import com.yjd.comm.settlement.service.IClearDayDetailService;
import com.yjd.comm.settlement.service.IClearDayPfService;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.settlement.dao.IClearDayPfDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：平台订单日结汇总业务实现<br>
 * <b>time</b>：2017-08-11 17:28:51 <br>
 * <b>author</b>：  yangchangyan@yijiedai.com
 */
@Service("clearDayPfService")
public class ClearDayPfServiceImpl extends BaseServiceImpl implements IClearDayPfService {

    /**
     * 插入
     *
     * @param model
     * @return
     * @throws Exception
     */
    public ClearDayPfModel insert(ClearDayPfModel model) throws Exception {
        return this.clearDayPfDao.insertModel(model, true);
    }

    /**
     * 更新
     *
     * @param model
     * @return
     * @throws Exception
     */
    public int update(ClearDayPfModel model) throws Exception {
        return this.clearDayPfDao.updateModel(model, true);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    public int delete(long id) throws Exception {
        Map<Object, Object> paramMap = new HashMap<Object, Object>();
        paramMap.put("id", id);
        return this.clearDayPfDao.deleteModel(paramMap, true);
    }

    /**
     * 生成平台日结算数据
     *
     * @param clearDate
     * @throws Exception
     */
    @Override
    public void generatePfClearDayData(Integer clearDate, Long currtime) throws Exception {
        if (clearDate == null) {
            FrameUtil.throwBaseException1("参数错误");
        }
        ClearDayPfModel pfClearDayModel = clearDayPfDao.getModelOne(FrameUtil.newHashMap("clear_date", clearDate), true);
        if (pfClearDayModel != null) {
            //如果已经存在则需要重新生成
            clearDayPfDao.deleteById(pfClearDayModel.getId(), true);
        }
        Double borrow_price = clearDayDetailService.statisticsPfBorrowPriceOfDay(clearDate);//支出金额
        Double locan_price = clearDayDetailService.statisticsPfLoanPriceOfDay(clearDate);//收入金额
        Double trade_price = clearDayDetailService.statisticsPfTradePriceOfDay(clearDate);//总成交额
        int trade_num = clearDayDetailService.statisticsPfTradeCountOfDay(clearDate);//成交笔数
        int refund_num = clearDayDetailService.statisticsPfRefundCountOfDay(clearDate);//退单笔数

        //设置运行日期的结算相关数据
        ClearDayPfModel clearDayPfModel = new ClearDayPfModel();
        clearDayPfModel.setBorrow_price(borrow_price);
        clearDayPfModel.setLoan_price(locan_price);
        clearDayPfModel.setTrade_price(trade_price);
        clearDayPfModel.setTrade_count(trade_num);
        clearDayPfModel.setRefund_count(refund_num);
        clearDayPfModel.setClear_date(clearDate.intValue());
        clearDayPfModel.setCreate_time(currtime);
        clearDayPfModel.setUpdate_time(currtime);
        clearDayPfModel.setStatus(0);
        clearDayPfDao.insertModel(clearDayPfModel, true);
    }

    /**
     * 生成平台日结算数据 (根据时间区间来)
     *
     * @param startDate
     * @param endDate
     * @param currtime
     * @throws Exception
     */
    @Override
    public void generatePfClearDayData(Integer startDate, Integer endDate, Long currtime) throws Exception {
        if (startDate == null || endDate == null || currtime == null) {
            FrameUtil.throwBaseException1("参数错误！");
        }

        if (startDate.intValue()>endDate.intValue()) {
            FrameUtil.throwBaseException1("开始日期不能小于结束日期");
        }

        Date date = DateUtil.timestampToDate(DateUtil
                .dateStringToTimestamp(DateUtil.timestampToDateString(FrameUtil.getTime(), DateUtil.PATTERN_yyyyMMdd), DateUtil.PATTERN_yyyyMMdd));
        int currtDate = Integer.parseInt(DateUtil.format(date, DateUtil.PATTERN_yyyyMMdd));

        if (startDate.intValue() >= currtDate) {
            FrameUtil.throwBaseException1("开始日期不能大于当前日期");
        }

        if (endDate.intValue() >= currtDate) {
            FrameUtil.throwBaseException1("结束日期不能大于等于当前日期");
        }


        do {
            generatePfClearDayData(startDate, currtime);//生成日结清算单
            Date next_date = DateUtil.addDay(DateUtil.parseDate(startDate + "", DateUtil.PATTERN_yyyyMMdd), 1);
            startDate = Integer.parseInt(DateUtil.format(next_date, DateUtil.PATTERN_yyyyMMdd));
            if (startDate > endDate) {
                return;
            }
        } while (true);


    }

    @Resource
    private IClearDayPfDao clearDayPfDao;

    @Override
    public IBaseDao getBaseDao() {
        return this.clearDayPfDao;
    }

    @Resource
    private IClearDayDetailService clearDayDetailService;
}
